<?php


namespace Home\DAO;


use Home\Common\FIdConst;

class TaobaoTradesDAO extends PSIBaseExDAO
{
    public function addorder(&$params)
    {
        $db = $this->db;

//        $id = $this->newId();
        $sql = "insert ignore into t_taobao_trades(adjust_fee, no_shipping, buyer_rate, cid,consign_time,discount_fee,divide_order_fee,invoice_no,is_daixiao,logistics_company,num,num_iid,oid,outer_sku_id,part_mjz_discount,
            payment, pic_path, price,refund_status,seller_rate,seller_type,shipping_type,sku_id,sku_properties_name,status,title,total_fee,tid,`type`,issd,sdtime,receiver_mobile,receiver_phone,
            receiver_name,buyer_nick,has_buyer_message,seller_nick, o2o_guide_id,o2o_guide_name,created,pay_time,modified,end_time,create_time)
            values ('%s', '%s', '%s', '%s','%s', '%s', '%s', '%s','%s', '%s', '%s','%s', '%s', '%s', '%s','%s', '%s', '%s','%s', '%s', '%s', '%s','%s', '%s', '%s', '%s',
            '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s')"; // 42

//        $sql = $db->prepare("insert into t_taobao_trades(no_shipping, adjust_fee, buyer_rate, cid,consign_time,discount_fee,divide_order_fee,invoice_no,is_daixiao,logistics_company,num,num_iid,oid,outer_sku_id,part_mjz_discount,
//            payment, pic_path, price,refund_status,seller_rate,seller_type,shipping_type,sku_id,sku_properties_name,status,title,total_fee,tid,`type`,issd,sdtime,create_time)
//            values (?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?,)"); // 32
//        $sql->bind_param("ssssssssssssssssssssssssssssss",
//            $param['no_shipping'], $param['adjust_fee'], $param['buyer_rate'],$param['cid'],$param['consign_time'], $param['discount_fee'],$param['divide_order_fee'],$param['invoice_no'],
//                $param['is_daixiao'],$param['logistics_company'],$param['num'],$param['num_iid'],$param['oid'],$param['outer_sku_id'],$param['part_mjz_discount'],
//                $param['payment'],$param['pic_path'],$param['price'],$param['refund_status'],$param['seller_rate'],$param['seller_type'],$param['shipping_type'],$param['sku_id'],
//                $param['sku_properties_name'],$param['status'],$param['title'],$param['total_fee'],$param['tid'],$param['type'],'raw',date('Y-m-d H:i:s'),date('Y-m-d H:i:s'));
        foreach ($params as $value)
        {

//            $param = array_merge($value['orders']['order'],$value);
//            unset($param['orders']);

            foreach ($value['orders']['order'] as $val) {
                $param = array_merge($value,$val);
                unset($param['orders']);
//                $sql->execute();
                $rc = $db->execute($sql,
                $param['adjust_fee'], $param['no_shipping'], $param['buyer_rate'],$param['cid'],$param['consign_time'], $param['discount_fee'],$param['divide_order_fee'],$param['invoice_no'],
                $param['is_daixiao'],$param['logistics_company'],$param['num'],$param['num_iid'],$param['oid'],$param['outer_sku_id'],$param['part_mjz_discount'],
                $param['payment'],$param['pic_path'],$param['price'],$param['refund_status'],$param['seller_rate'],$param['seller_type'],$param['shipping_type'],$param['sku_id'],
                $param['sku_properties_name'],$param['status'],$param['title'],$param['total_fee'],$param['tid'],$param['type'],'0', '0000-00-00 00:00:01',$param['receiver_mobile'],
                $param['receiver_phone'],$param['receiver_name'],$param['buyer_nick'],$param['has_buyer_message'],$param['seller_nick'],$param['o2o_guide_id'],$param['o2o_guide_name'],$param['created'],$param['pay_time'],
                    $param['modified'],
                    $param['end_time'],date('Y-m-d H:i:s') //42
            );//注意第一个值不能为bool类型，后面model.class.php会检测第一个值，是bool则没有预执行。

                if ($rc === false) {
                    return $this->sqlError(__METHOD__, __LINE__);
                }
//                var_dump($this->db->getLastSql());
            }

        }
//        $rc =
//        if ($rc === false) {
//            return $this->sqlError(__METHOD__, __LINE__);
//        }

        // 操作成功
//        $params["id"] = $id;
        return null;
    }
    //搜索当前会话用户作为卖家已卖出的增量交易数据（只能获取到三个月以内的交易信息）
    //$db->execute() 内匹配的是增量交易数据
    public function incrementv(&$params)
    {
        $db = $this->db;

//        $id = $this->newId();
        $sql = "insert into t_taobao_trades(no_shipping, adjust_fee, buyer_rate, cid,consign_time,discount_fee,divide_order_fee,end_time,invoice_no,is_daixiao,logistics_company,num,num_iid,oid,outer_sku_id,part_mjz_discount,
            payment, pic_path, price,refund_status,seller_rate,seller_type,shipping_type,sku_id,sku_properties_name,status,title,total_fee,tid,`type`,issd,sdtime,create_time)
            values ('%s', '%s', '%s', '%s','%s', '%s', '%s', '%s','%s', '%s', '%s', '%s','%s', '%s', '%s', '%s','%s', '%s', '%s', '%s','%s', '%s', '%s', '%s','%s', '%s', '%s', '%s',
            '%s', '%s',date('Y-m-d H:i:s'))";
        foreach ($params as $value)
        {

//            $param = array_merge($value['orders']['order'],$value);
//            unset($param['orders']);

            foreach ($value['orders']['order'] as $val) {
                $param = array_merge($value,$val);
                unset($param['orders']);
                var_dump($param);
            }
//            $db->execute($sql,
//                $param['no_shipping'], $param['adjust_fee'], $param['buyer_rate'],$param['cid'],$param['consign_time'], $param['discount_fee'],$param['divide_order_fee'],$param['end_time'],
//                $param['invoice_no'],$param['is_daixiao'],$param['logistics_company'],$param['num'],$param['num_iid'],$param['oid'],$param['outer_sku_id'],$param['part_mjz_discount'],
//                $param['payment'],$param['pic_path'],$param['price'],$param['refund_status'],$param['seller_rate'],$param['seller_type'],$param['shipping_type'],$param['sku_id'],
//                $param['sku_properties_name'],$param['status'],$param['title'],$param['total_fee'],$param['tid'],$param['type'],$param['issd']
//            );
        }
//        $rc =
//        if ($rc === false) {
//            return $this->sqlError(__METHOD__, __LINE__);
//        }

        // 操作成功
//        $params["id"] = $id;
        return null;
    }
    public function tbbillList($params)
    {
        $db = $this->db;

        $loginUserId = $params["loginUserId"];
        if ($this->loginUserIdNotExists($loginUserId)) {
            return $this->emptyResult();
        }

        $start = $params["start"];
        $limit = $params["limit"];

        $seller_nick = $params["seller_nick"];
        $tid = $params["tid"];
        $fromcreated = $params["fromcreated"];
        $tocreated = $params["tocreated"];

        $sql = "select adjust_fee, no_shipping, buyer_rate, cid,consign_time,discount_fee,divide_order_fee,invoice_no,is_daixiao,logistics_company,num,num_iid,oid,outer_sku_id,part_mjz_discount,
            payment, pic_path, price,refund_status,seller_rate,seller_type,shipping_type,sku_id,sku_properties_name,status,title,total_fee,tid,`type`,issd,sdtime,receiver_mobile,receiver_phone,
            receiver_name,buyer_nick,has_buyer_message, seller_nick,o2o_guide_id,o2o_guide_name,created,pay_time,modified,end_time,create_time, update_time
            from t_taobao_trades ";
        $queryParams = [];

        $wheresql = '';
//        $ds = new DataOrgDAO($db);
//        $rs = $ds->buildSQL(FIdConst::SALE_REJECTION, "t_role_permission_dataorg", $loginUserId); //数据域
//        if ($rs) {
//            $wheresql .= " and " . $rs[0];
//            $queryParams = $rs[1];
//        }

        if ($seller_nick != -1) {
            $wheresql .= " and (seller_nick = '%s') ";
//            $queryParams[] = 'testx';
            $queryParams[] = $seller_nick ;
        }
        if ($tid) {
//            $wheresql .= " and (tid like '%s') ";
//            $queryParams[] = "%{$tid}%";
            $wheresql .= " and (tid = '%s') ";
            $queryParams[] = $tid;
        }
        if ($fromcreated) {
            $wheresql .= " and (created >= '%s') ";
            $queryParams[] = $fromcreated;
        }
        if ($tocreated) {
            $wheresql .= " and (created <= '%s') ";
            $queryParams[] = $tocreated;
        }

        if ($wheresql != ''){
            $wheresql = ' where ' . substr($wheresql, 4);
            $sql .= $wheresql;
        }
        $countsql = "select count(*) as total from t_taobao_trades " . $wheresql ;
        $countdata = $db->query($countsql, $queryParams);
        $sql .= " order by created desc
              limit %d, %d";
        $queryParams[] = $start;
        $queryParams[] = $limit;
        $data = $db->query($sql, $queryParams);
        return [
            "dataList" => $data,
            "totalCount" => $countdata['0']['total']
        ];
    }
    public function updateorder(&$params)
    {
        $db = $this->db;
//        $id = $this->newId();
        $sql = "update t_taobao_trades set adjust_fee = '%s', no_shipping = '%s', buyer_rate = '%s', cid = '%s',consign_time = '%s',discount_fee = '%s',divide_order_fee = '%s',invoice_no = '%s',is_daixiao = '%s',logistics_company = '%s',num = '%s',num_iid = '%s',outer_sku_id = '%s',part_mjz_discount = '%s',
            payment = '%s', pic_path = '%s', price = '%s',refund_status = '%s',seller_rate = '%s',seller_type = '%s',shipping_type = '%s',sku_id = '%s',sku_properties_name = '%s',status = '%s',title = '%s',total_fee = '%s',tid = '%s',`type` = '%s',receiver_mobile = '%s',receiver_phone = '%s',
            receiver_name = '%s',buyer_nick = '%s',has_buyer_message = '%s',o2o_guide_id = '%s',o2o_guide_name = '%s',created = '%s',pay_time = '%s',modified = '%s',end_time = '%s' where oid = '%s'"; // no sdtime,issd

        foreach ($params as $value)
        {
            foreach ($value['orders']['order'] as $val) {
                $param = array_merge($value,$val);
                unset($param['orders']);
//                $sql->execute();
                $rc = $db->execute($sql,
                    $param['adjust_fee'], $param['no_shipping'], $param['buyer_rate'],$param['cid'],$param['consign_time'], $param['discount_fee'],$param['divide_order_fee'],$param['invoice_no'],
                    $param['is_daixiao'],$param['logistics_company'],$param['num'],$param['num_iid'],$param['outer_sku_id'],$param['part_mjz_discount'],
                    $param['payment'],$param['pic_path'],$param['price'],$param['refund_status'],$param['seller_rate'],$param['seller_type'],$param['shipping_type'],$param['sku_id'],
                    $param['sku_properties_name'],$param['status'],$param['title'],$param['total_fee'],$param['tid'],$param['type'],$param['receiver_mobile'],
                    $param['receiver_phone'],$param['receiver_name'],$param['buyer_nick'],$param['has_buyer_message'],$param['o2o_guide_id'],$param['o2o_guide_name'],$param['created'],$param['pay_time'],
                    $param['modified'],
                    $param['end_time'],$param['oid'] //
                );//注意第一个值不能为bool类型，后面model.class.php会检测第一个值，是bool则没有预执行。

                if ($rc === false) {
                    return $this->sqlError(__METHOD__, __LINE__);
                }
//                var_dump($this->db->getLastSql());
            }
        }
//        $rc =
//        if ($rc === false) {
//            return $this->sqlError(__METHOD__, __LINE__);
//        }

        // 操作成功
//        $params["id"] = $id;
        return null;
    }
}